package com.itheima.controller;

import com.itheima.dto.EmpDto;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /*@GetMapping
    public Result getByPage(Integer page,Integer pageSize){
        PageBean<Emp> pageBean = empService.getByPage(page, pageSize);
        return Result.success(pageBean);
    }*/

    @GetMapping
    public Result getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam (defaultValue = "10") Integer pageSize, EmpDto dto){
        PageBean<Emp> pageBean = empService.getByPage(page, pageSize,dto);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable Integer[] ids){
        int i=empService.deleteEmp(ids);
        return i>0?Result.success():Result.error(Arrays.toString(ids)+"删除失败!");
    }

    @PostMapping
    public Result insertEmp(@RequestBody Emp emp){
        int i=empService.insertEmp(emp);
        return i>0?Result.success():Result.error("id为"+emp.getId()+"的员工添加失败");
    }

}
